﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;

namespace ZFine.Application.Project
{
    public class SalaryApp
    {
        private  IPersonSalaryRepository psService=new PersonSalaryRepository();
        private ISalaryRepository service = new SalaryRepository();
        private IRoleRepository roleApp = new RoleRepository();
        private  IUserRepository useApp=new UserRepository();
        public List<SalaryEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<SalaryEntity> GetList(Expression<Func<SalaryEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }
        public List<SalaryEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<SalaryEntity>();
            //expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }

        public RoleEntity FindEntityNew(string keyValue)
        { 
            RoleEntity result = roleApp.FindEntity(keyValue);
            return result;
        }

        public List<SalaryEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<SalaryEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public SalaryEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        public int SubmitForm(SalaryEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }

        /// <summary>
        /// 统计运量查询
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public List<SalaryResult> GetSalary(Pagination pagination,string keyWord,string StartTime,string EndTime)
        {
            List<SalaryResult> result=new List<SalaryResult>();
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                var expression = ExtLinq.True<UserEntity>();
                expression = expression.And(f => f.F_RealName.Contains(keyWord));
                
                //var person = useApp.FindEntity(expression);
                var person = useApp.FindList(expression,pagination);
                for (int i = 0; i < person.Count; i++)
                {
                    if (!string.IsNullOrEmpty(StartTime))
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' and F_CreatorTime<'{2}' group by Persion_F_Id ", person[i].F_Id, StartTime, EndTime);
                            //result = service.GetSalaryList(sql);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' group by Persion_F_Id ", person[i].F_Id, StartTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and Persion_F_Id='{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", person[i].F_Id, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and Persion_F_Id='{0}' group by Persion_F_Id ", person[i].F_Id);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(StartTime))
                {
                    //DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql1 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and F_CreatorTime> '{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", StartTime, EndTime);
                        result = service.GetSalaryList(sql1);
                    }
                    else
                    {
                        string sql1 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and F_CreatorTime> '{0}' group by Persion_F_Id ", StartTime);
                        result = service.GetSalaryList(sql1);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql2 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 and F_CreatorTime<'{0}' group by Persion_F_Id ", EndTime);
                        result = service.GetSalaryList(sql2);
                    }
                    else
                    {
                        string sql2 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=0 group by Persion_F_Id ");
                        result = service.GetSalaryList(sql2);
                    }
                    
                }
                
            }
            
            return result;
        }



        /// <summary>
        /// 放行查询
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public List<SalaryResult> GetFangXingSalary(Pagination pagination,string keyWord, string StartTime, string EndTime)
        {
            List<SalaryResult> result = new List<SalaryResult>();
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                var expression = ExtLinq.True<UserEntity>();
                expression = expression.And(f => f.F_RealName.Contains(keyWord));
                //var person = useApp.FindEntity(expression);
                var person = useApp.FindList( expression,pagination);//找到包含关键字的用户的列表
                for (int i = 0; i < person.Count; i++)               //根据找到的用户的id 查找对应的工资数据 存到result里
                {
                    if (!string.IsNullOrEmpty(StartTime))
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' and F_CreatorTime<'{2}' group by Persion_F_Id ", person[i].F_Id, StartTime, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' group by Persion_F_Id ", person[i].F_Id, StartTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and Persion_F_Id='{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", person[i].F_Id, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and Persion_F_Id='{0}' group by Persion_F_Id ", person[i].F_Id);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(StartTime))
                {
                    //DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql1 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and F_CreatorTime> '{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", StartTime, EndTime);
                        result = service.GetSalaryList(sql1);
                    }
                    else
                    {
                        string sql1 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and F_CreatorTime> '{0}' group by Persion_F_Id ", StartTime);
                        result = service.GetSalaryList(sql1);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql2 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 and F_CreatorTime<'{0}' group by Persion_F_Id ", EndTime);
                        result = service.GetSalaryList(sql2);
                    }
                    else
                    {
                        string sql2 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay,sum(OperCarNum) as TotalCarNum from PersonSalary where State = 1 and SalaryType=1 group by Persion_F_Id ");
                        result = service.GetSalaryList(sql2);
                    }

                }

            }

            return result;
        }


        /// <summary>
        /// 工资总表查询
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public List<SalaryResult> GetTotalSalary(Pagination pagination,string keyWord, string StartTime, string EndTime)
        {
            List<SalaryResult> result = new List<SalaryResult>();
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                var expression = ExtLinq.True<UserEntity>();
                expression = expression.And(f => f.F_RealName.Contains(keyWord));
                //var person = useApp.FindEntity(expression);
                var person = useApp.FindList( expression,pagination);//找到包含关键字的用户的列表
                for (int i = 0; i < person.Count; i++)               //根据找到的用户的id 查找对应的工资数据 存到result里
                {
                    if (!string.IsNullOrEmpty(StartTime))
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' and F_CreatorTime<'{2}' group by Persion_F_Id ", person[i].F_Id, StartTime, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' group by Persion_F_Id ", person[i].F_Id, StartTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and Persion_F_Id='{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", person[i].F_Id, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and Persion_F_Id='{0}' group by Persion_F_Id ", person[i].F_Id);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(StartTime))
                {
                    //DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql1 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and F_CreatorTime> '{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", StartTime, EndTime);
                        result = service.GetSalaryList(sql1);
                    }
                    else
                    {
                        string sql1 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and F_CreatorTime> '{0}' group by Persion_F_Id ", StartTime);
                        result = service.GetSalaryList(sql1);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql2 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1  and F_CreatorTime<'{0}' group by Persion_F_Id ", EndTime);
                        result = service.GetSalaryList(sql2);
                    }
                    else
                    {
                        string sql2 = string.Format("select Persion_F_Id,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 group by Persion_F_Id ");
                        result = service.GetSalaryList(sql2);
                    }

                }

            }

            return result;
        }


        /// <summary>
        /// 统计运量查询
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public List<SalaryResult> GetSalaryDuiwei(Pagination pagination, string keyWord, string StartTime, string EndTime)
        {
            List<SalaryResult> result = new List<SalaryResult>();
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                var expression = ExtLinq.True<UserEntity>();
                expression = expression.And(f => f.F_RealName.Contains(keyWord));
                //var person = useApp.FindEntity(expression);
                var person = useApp.FindList(expression, pagination);
                for (int i = 0; i < person.Count; i++)
                {
                    if (!string.IsNullOrEmpty(StartTime))
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' and F_CreatorTime<'{2}' group by Persion_F_Id ", person[i].F_Id, StartTime, EndTime);
                            //result = service.GetSalaryList(sql);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and Persion_F_Id='{0}' and F_CreatorTime> '{1}' group by Persion_F_Id ", person[i].F_Id, StartTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(EndTime))
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and Persion_F_Id='{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", person[i].F_Id, EndTime);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                        else
                        {
                            string sql = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and Persion_F_Id='{0}' group by Persion_F_Id ", person[i].F_Id);
                            var res = service.GetSalaryListF(sql);
                            result.Add(res);
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(StartTime))
                {
                    //DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql1 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and F_CreatorTime> '{0}' and F_CreatorTime<'{1}' group by Persion_F_Id ", StartTime, EndTime);
                        result = service.GetSalaryList(sql1);
                    }
                    else
                    {
                        string sql1 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and F_CreatorTime> '{0}' group by Persion_F_Id ", StartTime);
                        result = service.GetSalaryList(sql1);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        //DateTime eTime = Convert.ToDateTime(EndTime);
                        string sql2 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 and F_CreatorTime<'{0}' group by Persion_F_Id ", EndTime);
                        result = service.GetSalaryList(sql2);
                    }
                    else
                    {
                        string sql2 = string.Format("select Persion_F_Id,sum(Weight) as Weight,sum(DWeight) as DWeight,sum(EveryTimeSalary) as NeedToPay from PersonSalary where State = 1 and SalaryType=2 group by Persion_F_Id ");
                        result = service.GetSalaryList(sql2);
                    }

                }

            }

            return result;
        }



    }
}
